<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>任务管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/taskList' }"
        >任务列表</el-breadcrumb-item
      >
      <el-breadcrumb-item>任务详情</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 任务详情 -->
    <el-card shadow="hover">
      <div slot="header" class="clearfix">
        <h1>任务详情</h1>
      </div>

      <el-form
        label-position="left"
        label-width="120px"
        class="demo-table-expand"
      >
        <el-form-item label="任务id">
          <span>{{ auditList._id }}</span>
        </el-form-item>
        <el-form-item label="任务名称">
          <span>{{ auditList.taskname }}</span>
        </el-form-item>
        <el-form-item label="创建人" v-if="auditList.creator">
          <span>
            {{ auditList.creator.name }}
          </span>
        </el-form-item>
        <el-form-item label="负责人" v-if="auditList.worker">
          <span v-for="(item, index) in auditList.worker" :key="index">
            {{ item.name }}
          </span>
        </el-form-item>
        <el-form-item label="任务等级">
          <span v-if="auditList.grade === 0">顶级</span>
          <span v-if="auditList.grade === 1">二级</span>
          <span v-if="auditList.grade === 2">三级</span>
          <span v-if="auditList.grade === 3">四级</span>
          <span v-if="auditList.grade === 4">五级</span>
        </el-form-item>

        <el-form-item label="审核状态">
          <span v-if="auditList.status === 0">进行中</span>
          <span v-if="auditList.status === 1">审核中</span>
          <span class="wei" v-if="auditList.status === 2">被打回</span>
          <span v-if="auditList.status === 3">已完成</span>
        </el-form-item>
        <el-form-item label="任务描述">
          <span>
            {{ auditList.describe }}
          </span>
        </el-form-item>
        <el-form-item label="任务附件">
          <span v-for="(item, index) in auditList.fileAddress" :key="index">
            <span v-if="item.files.length != 0">
              <span v-for="(item2, index) in item.files" :key="index">
                <el-tag
                  disable-transitions
                  effect="dark"
                  @click="fileClick(item2)"
                  v-if="item2 != null"
                >
                  {{ item2.originalname }}
                </el-tag>
              </span>
            </span>
          </span>
          <span>[可点击下载]</span>
        </el-form-item>
        <el-form-item label="任务结束时间">
          <span>{{ auditList.endtime | date }}</span>
        </el-form-item>
        <el-form-item label="创建时间">
          <span>{{ auditList.updatedAt | date }}</span>
        </el-form-item>
      </el-form>

      <!-- 审核按钮 -->
      <!-- <el-button
        type="primary"
        size="default"
        @click="submitConfirm()"
        v-if="auditList.status == 0 || auditList.status == 2"
        >提交审核</el-button
      >
      <el-button
        type="primary"
        size="default"
        @click="examineConfirm()"
        v-if="auditList.status == 1"
        >确认审核</el-button
      >
      <el-button
        type="warning"
        size="default"
        @click="returnExamine()"
        v-if="auditList.status == 1"
        >退回审核</el-button
      > -->
      <el-button @click="$router.go(-1)" type="primary">返回</el-button>
    </el-card>

    <!-- 任务动态 -->
    <el-card shadow="hover" class="record">
      <div slot="header" class="clearfix">
        <h1>任务动态</h1>
      </div>
      <!-- journal -->
      <!-- <el-table :data="taskDynamics">
        <el-table-column prop="" label="操作时间">
          <template slot-scope="scope">
            <div>
              {{ scope.row.createdAt | date }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="任务名称">
          <template slot-scope="scope">
            <span>
              {{ scope.row.taskname }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="" label="操作人">
          <template slot-scope="scope">
            <span v-if="scope.row.userId">
              <span>
                {{ scope.row.userId.name }}
              </span>
            </span>
            <span v-else> 管理员 </span>
          </template>
        </el-table-column>
        <el-table-column label="操作类型">
          <template slot-scope="scope">
            <div v-if="scope.row.operate === 0">提交任务</div>
            <div v-if="scope.row.operate === 1">更新任务</div>
            <div v-if="scope.row.operate === 2">添加任务</div>
            <div v-if="scope.row.operate === 3">删除任务</div>
            <div v-if="scope.row.operate === 4">任务审核通过</div>
            <div v-if="scope.row.operate === 5">打回任务</div>
            <div v-if="scope.row.operate === 6">神秘</div>
          </template>
        </el-table-column>
      </el-table> -->

      <!-- notice -->
      <el-table :data="taskDynamics">
        <el-table-column prop="" label="操作时间">
          <template slot-scope="scope">
            <div>
              {{ scope.row.createdAt | date }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="操作人">
          <template slot-scope="scope">
            <span v-if="scope.row.userId.name">
              <span>
                {{ scope.row.userId.name }}
              </span>
            </span>
            <span v-else> 管理员 </span>
          </template>
        </el-table-column>

        <el-table-column prop="" label="通知人">
          <template slot-scope="scope">
            <span v-for="(item, index) in scope.row.notifier" :key="index">
              {{ item.name }}
            </span>
          </template>
        </el-table-column>

        <el-table-column label="操作类型">
          <template slot-scope="scope">
            <div v-if="scope.row.operate === 0">提交任务</div>
            <div v-if="scope.row.operate === 1">更新任务</div>
            <div v-if="scope.row.operate === 2">添加任务</div>
            <div v-if="scope.row.operate === 3">删除任务</div>
            <div v-if="scope.row.operate === 4">任务审核通过</div>
            <div v-if="scope.row.operate === 5">打回任务</div>
            <div v-if="scope.row.operate === 6">提醒</div>
          </template>
        </el-table-column>

        <el-table-column prop="" label="动态描述" width="450px">
          <template slot-scope="scope" class="aa">
            <span v-html="scope.row.tips"></span>
          </template>
        </el-table-column>

         <el-table-column label="操作" width="80px">
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="noticeDel(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        background
        layout="total, prev, pager, next"
        :total="total"
        style="margin-top: 20px"
        :current-page="queryInfo.pagenum"
        :page-size="queryInfo.pagesize"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>

     <!-- 提交记录 -->
    <el-card shadow="hover" class="record">
      <div slot="header" class="clearfix">
        <h1>任务提交记录</h1>
      </div>

      <el-table
        :data="tableData"
        highlight-current-row
        style="width: 100%"
        row-key="_id"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="" label="提交时间">
          <template slot-scope="scope">
            <div>
              {{ scope.row.createdAt | date }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="提交人" align="center">
        </el-table-column>

        <el-table-column prop="process" label="任务进度" align="center">
        </el-table-column>

        <el-table-column prop="describe" label="提交描述" align="center">
        </el-table-column>

        <el-table-column label="附件">
          <template v-slot="scope">
            <!-- <span v-if="scope.row.length != 0"> -->
            <span v-for="(item, index) in scope.row" :key="index">
              <span v-for="(item1, index1) in item" :key="index1">
                <span v-if="item1.originalname != null">
                  <el-tag
                    disable-transitions
                    @click="fileClick(item1)"
                    effect="dark"
                  >
                    {{ item1.originalname }}
                  </el-tag>
                </span>
              </span>
            </span>
            <!-- </span> -->
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="danger" size="mini" @click="recordDel(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import dayjs from "dayjs";
export default {
  filters: {
    date(val) {
      return dayjs(val).format("YYYY-MM-DD HH:mm:ss");
    },
    describe(val) {
      // var data = val.split("<h3>")[1];
      // return val;
    },
  },
  props: {
    id: "",
  },
  data() {
    return {
      auditList: {},
      tableData: [],
      taskDynamics: [],
      queryInfo: {
        status: this.$route.query.status * 1 || 0,
        pagenum: this.$route.query.pagenum * 1 || 1, //第几页
        pagesize: 7, //每页显示个数
      },
      //总条数
      total: 0,
    };
  },
  methods: {
    //获取单个任务详情信息
    async getAuditList() {
      let res = await this.$http.get(`/examine/seeExamine/${this.id}`);
      this.auditList = res.data.data.data;
      this.tableData = res.data.data.data.fileAddress;
      this.tableData.reverse();
    },

    // 文件下载
    async fileClick(val) {
      this.$confirm(`确认要下载文件吗, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "下载成功!",
          });
          window.open(
            `https://quanquan.asia/admin/api/files/downloadFile?path=${val.path}&originalname=${val.originalname}`,
            "_self"
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消下载",
          });
        });
    },

    // 删除提交记录
    async recordDel(val) {
      this.$confirm(`确认要删除此记录吗, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          await this.$http.delete(`/examine/recordDel/${this.id}`, {
            data: {
              id: val._id,
            },
          });
          await this.getAuditList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 删除任务动态
    async noticeDel(val) {
      this.$confirm(`是否确定删除该消息`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await this.$http.get(`/journal/delNotice/${val._id}`);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getTaskDynamics();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //获取任务动态
    async getTaskDynamics() {
      let res = await this.$http.get(`/journal/taskDynamics/${this.id}`, {
        params: this.queryInfo,
      });
      let data = res.data;
      if (data.status === 200) {
        this.taskDynamics = data.data.data;
        this.queryInfo.pagenum = data.data.pagenum;
        this.total = data.data.total;
      }
    },

    //页数改变触发事件
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.$router.replace({
        query: {
          status: this.$route.query.status,
          pagenum: val,
        },
      });
      this.getTaskDynamics();
    },
  },
  created() {
    this.getAuditList();
    this.getTaskDynamics();
  },
};
</script>

<style lang="less" scoped>
.wei {
  color: red;
  font-weight: bold;
}
.el-tag {
  margin: 0 10px 0 0;
}
.el-form-item {
  margin-bottom: 5px;
}

.record {
  margin-top: 15px;
}

.el-table .cell h3{
  color: red;
}

/deep/ h3 {
    display: inline-block;
}
</style>